package com.feige.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.constant.Constants;
import com.feige.dao.SignTaskMapper;
import com.feige.pojo.NotifyMsg;
import com.feige.pojo.SignTask;
import com.feige.pojo.vo.SignStatus;
import com.feige.service.SignTaskService;
import com.feige.utils.aliyun.AliyunOssHandler;
import com.feige.utils.qrcode.QrCodeUtils;
import com.feige.result.AjaxResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Service
public class SignTaskServiceImpl extends ServiceImpl<SignTaskMapper, SignTask> implements SignTaskService {

    @Resource
    private RabbitTemplate rabbitTemplate;


    @Override
    public boolean save(SignTask signTask) {
        boolean save = false;
        NotifyMsg notifyMsg = new NotifyMsg();
        notifyMsg
                .setType(1)
                .setCourseId(signTask.getCourseId());
        if (signTask.getType().equals(1)){
            save = super.save(signTask);
            if (save){
                notifyMsg
                        .setContent("一键")
                        .setExtend(signTask.getId());
            }
        } else if (signTask.getType().equals(2)) {
            String fileName = IdUtil.simpleUUID() + ".jpg";
            String name = AliyunOssHandler.joinFileName(QrCodeUtils.SIGN_FILE_TYPE_NAME, fileName);
            String signQrCodePath = AliyunOssHandler.BUCKET_URL + name;
            signTask.setExtend(signQrCodePath);
            save = super.save(signTask);
            if (save){
                save = QrCodeUtils.generateQrCodeSaveOss(Constants.SIGN_QR_CODE_URL_PREFIX + signTask.getId(), name);
                if (save){
                    notifyMsg
                            .setExtend(signTask.getId())
                            .setContent("扫码");
                }
            }
        }else if (signTask.getType().equals(3)){
            save = super.save(signTask);
            if (save){
                notifyMsg
                        .setContent("手势")
                        .setExtend(signTask.getExtend() + ":" + signTask.getId());
            }
        }
        if (save){
            notifyMsg.setExtend(notifyMsg.getExtend() + ":" + signTask.getSignBeginTime().getTime() + ":" + signTask.getSignEndTime().getTime());
            rabbitTemplate.convertAndSend("DirectExchange","DirectRouting",notifyMsg);
        }
        return save;
    }


    @Override
    public AjaxResult getSignStatus(String userId, String courseId, Long pageNum) {
        Page<SignStatus> page = new Page<>(pageNum, Constants.PAGE_SIZE * 2L);
        this.baseMapper.getSignStatus(page,userId,courseId);
        AjaxResult success = AjaxResult.success(page.getRecords());
        success.put("total",page.getTotal());
        return success;
    }
}
